<?php

class Gamer
{
	private $buffer;
	private $username;
	private $get = array();
	
	public function Gamer($username)
	{
		$this->username = $username;
		
		// get page
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $this->username . ".gamer.ru/posts");
		curl_setopt($curl, CURLOPT_HEADER, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		$this->buffer = curl_exec($curl);
		curl_close($curl);
		
		// get name
		preg_match("'<h4><span>([0-9]+)</span> уровень</h4>'i", $this->buffer, $matches);
		$this->get['level'] = $matches[1];

		// get top place
		preg_match("#([0-9]+)-е место в топе игроков#i", $this->buffer, $matches);
		if(count($matches))
		{
			$this->get['place'] = $matches[1];
		}
		else
		{
			$this->get['place'] = -1;
		}
		
		// get grade
		preg_match("# уровень</h4> <span>(.+)</span>#i", $this->buffer, $matches);
		$this->get['grade'] = $matches[1];	
		
		// get posts
		preg_match("#([0-9]+) (постов|поста|пост)<br />#i", $this->buffer, $matches);
		$this->get['posts'] = $matches[1];
		
		// get comments
		preg_match("#([0-9]+) (комментариев|комментария|комментарий)<br />#i", $this->buffer, $matches);
		$this->get['comments'] = $matches[1];	

		// get silver posts
		preg_match("#<a href=\"http://" . $this->username . ".gamer.ru/posts/silver\">([0-9]+) се#i", $this->buffer, $matches);
		$this->get['silver'] = $matches[1];	
		
		// get golden posts
		preg_match("#<a href=\"http://" . $this->username . ".gamer.ru/posts/golden\">([0-9]+) зо#i", $this->buffer, $matches);
		$this->get['golden'] = $matches[1];	
		
		// get diamond posts
		preg_match("#<a href=\"http://" . $this->username . ".gamer.ru/posts/brilliant\">([0-9]+) бри#i", $this->buffer, $matches);
		$this->get['diamond'] = $matches[1];
	}

	public function GetName() 
	{ 
		return $this->username; 
	}
	
	public function GetLevel() 
	{ 
		return $this->get['level']; 
	}
	
	public function GetGrade() 
	{ 
		return $this->get['grade']; 
	}
		
	public function GetPlace() 
	{ 
		return $this->get['place']; 
	}
	
	public function GetPosts($type) 
	{ 
		switch($type)
		{
			case 'diamond':
			{
				return $this->get['diamond']; 
			}
			break;
			case 'golden':
			{
				return $this->get['golden']; 
			}
			break;		
			case 'silver':
			{
				return $this->get['silver']; 
			}
			break;
			default:
			{
				return $this->get['posts']; 
			}
			break;
		}
	}
	
	public function GetComments() 
	{ 
		return $this->get['comments']; 
	}
}

?>